/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月16日
 */
package com.massyframework.beanskin.web.filter;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.web.utils.WebUtils;

/**
 * {@link ModuleFilter}, 提供{@link #getAssociatedModule()}方法，
 * 支持将{@link FilterConfig}生成{@link ModuleFilterConfig}以支持装配件。
 * 
 * @author huangkh
 *
 */
abstract class ModuleFilter implements Filter {

	private ModuleFilterConfig config;

    /**
     * 构造方法
     */
    public ModuleFilter() {
    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
     */
    @Override
    public final void init(FilterConfig filterConfig) throws ServletException {
        try {
            Module<?> module = WebUtils.retrieveAssociateModule(filterConfig);
            this.config = this.createFilterConfig(module, filterConfig);
            this.init();
        }catch(Exception e){
            throw new ServletException(e.getMessage(), e);
        }
    }

    /**
     * 初始化
     */
    protected void init() throws ServletException{

    }

    /* (non-Javadoc)
     * @see javax.servlet.Filter#destroy()
     */
    @Override
    public void destroy() {
    }

    /**
     * 过滤器配置
     * @return {@link FilterConfig}
     */
    public FilterConfig getFilterConfig() {
        return this.config;
    }

    /**
     * 关联的模块
     * @return {@link ServiceExportableModule}
     */
    public Module<?> getAssociatedModule() {
        return this.config == null ? null: this.config.getAssociatedModule();
    }

    /**
     * Servlet上下文
     * @return {@link ServletContext}
     */
    protected ServletContext getServletContext() {
        return this.config == null ? null : this.config.getServletContext();
    }

    /**
     * 创建{@link FilterConfig}
     * @param assembly {@link Module}, 模块
     * @param config {@link FilterConfig},外部传入的FilterConfig
     * @return {@link FilterConfig}
     */
    protected ModuleFilterConfig createFilterConfig(Module<?> module, FilterConfig config) {
        ModuleFilterConfig result =
                new ModuleFilterConfig(module, config);
        return result;
    }
    
    

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return this.getAssociatedModule() != null ?
                this.getClass().getSimpleName() + " [assembly=" + this.getAssociatedModule() + "]" :
                this.getClass().getSimpleName();
    }
}
